<html>
<head>
<title>MainActivity.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.ln { color: #999999; font-weight: normal; font-style: normal; }
.s0 { color: rgb(0,0,128); font-weight: bold; }
.s1 { color: rgb(0,0,0); }
.s2 { color: rgb(0,128,0); font-weight: bold; }
.s3 { color: rgb(128,128,128); font-style: italic; }
</style>
</head>
<BODY BGCOLOR="#ffffff">
<TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#c0c0c0" >
<TR><TD><CENTER>
<FONT FACE="Arial, Helvetica" COLOR="#000000">
MainActivity.java</FONT>
</center></TD></TR></TABLE>
<pre>
<span class="s0">package </span><span class="s1">com.example.administrator.androidui4; 
 
</span><span class="s0">import </span><span class="s1">android.app.ProgressDialog; 
</span><span class="s0">import </span><span class="s1">android.content.DialogInterface; 
</span><span class="s0">import </span><span class="s1">android.support.v7.app.AlertDialog; 
</span><span class="s0">import </span><span class="s1">android.support.v7.app.AppCompatActivity; 
</span><span class="s0">import </span><span class="s1">android.os.Bundle; 
</span><span class="s0">import </span><span class="s1">android.view.View; 
</span><span class="s0">import </span><span class="s1">android.widget.Button; 
</span><span class="s0">import </span><span class="s1">android.widget.RadioButton; 
</span><span class="s0">import </span><span class="s1">android.widget.RadioGroup; 
</span><span class="s0">import </span><span class="s1">android.widget.Toast; 
 
</span><span class="s0">import static </span><span class="s1">com.example.administrator.androidui4.R.id.btn_my; 
</span><span class="s0">import static </span><span class="s1">com.example.administrator.androidui4.R.id.btn_my; 
 
</span><span class="s0">public class </span><span class="s1">MainActivity </span><span class="s0">extends </span><span class="s1">AppCompatActivity { 
    String[] furits=</span><span class="s0">new </span><span class="s1">String[]{</span><span class="s2">&quot;苹果&quot;</span><span class="s1">,</span><span class="s2">&quot;西瓜&quot;</span><span class="s1">,</span><span class="s2">&quot;桃子&quot;</span><span class="s1">}; 
 
</span><span class="s0">class </span><span class="s1">MyButtonListener </span><span class="s0">implements </span><span class="s1">View.OnClickListener{ 
 
 
    @Override 
    </span><span class="s0">public void </span><span class="s1">onClick(View v) { 
        Toast.makeText(MainActivity.</span><span class="s0">this</span><span class="s1">, </span><span class="s2">&quot;内部类点击事件处理&quot;</span><span class="s1">, Toast.LENGTH_SHORT).show(); 
    } 
} 
    @Override 
    </span><span class="s0">protected void </span><span class="s1">onCreate(Bundle savedInstanceState) { 
        </span><span class="s0">super</span><span class="s1">.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
 
 
      Button btnMyDialog = findViewById(R.id.btn_my); 
      btnMyDialog.setOnClickListener(</span><span class="s0">new </span><span class="s1">View.OnClickListener() { 
          @Override 
          </span><span class="s0">public void </span><span class="s1">onClick(View v) { 
              MyDialog dialog = </span><span class="s0">new </span><span class="s1">MyDialog(MainActivity.</span><span class="s0">this</span><span class="s1">); 
              dialog.show(); 
          } 
      }); 
 
 
        Button btnprogressDialog = findViewById(R.id.btn_progress_dialog);</span><span class="s3">//获取这个按钮</span><span class="s1"> 
        btnprogressDialog.setOnClickListener(</span><span class="s0">new </span><span class="s1">View.OnClickListener() { 
            @Override 
            </span><span class="s0">public void </span><span class="s1">onClick(View v) { 
                ProgressDialog progressDialog=</span><span class="s0">new </span><span class="s1">ProgressDialog(MainActivity.</span><span class="s0">this</span><span class="s1">); 
                progressDialog.setMessage(</span><span class="s2">&quot;加载中。。。。&quot;</span><span class="s1">); 
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
                progressDialog.show(); 
 
            } 
        }); 
 
    Button button1=findViewById(R.id.btn_button1); 
 
    button1.setOnClickListener(</span><span class="s0">new </span><span class="s1">View.OnClickListener() { 
 
        @Override 
 
        </span><span class="s0">public void </span><span class="s1">onClick(View v) { 
            Toast.makeText(MainActivity.</span><span class="s0">this</span><span class="s1">, 
                    </span><span class="s2">&quot;匿名内部类对象&quot;</span><span class="s1">, 
                    Toast.LENGTH_SHORT).show(); 
        } 
    }); 
    Button button2=findViewById(R.id.btn_button2); 
    button2.setOnClickListener(</span><span class="s0">new </span><span class="s1">MyButtonListener()); 
 
RadioGroup rgsex=findViewById(R.id.rg_sex); 
rgsex.setOnCheckedChangeListener(</span><span class="s0">new </span><span class="s1">RadioGroup.OnCheckedChangeListener() { 
    @Override 
    </span><span class="s0">public void </span><span class="s1">onCheckedChanged(RadioGroup group, </span><span class="s0">int </span><span class="s1">checkedId) { 
        Toast.makeText(MainActivity.</span><span class="s0">this</span><span class="s1">,  checkedId+</span><span class="s2">&quot;&quot;</span><span class="s1">, Toast.LENGTH_SHORT).show(); 
    </span><span class="s0">if</span><span class="s1">(checkedId == R.id.rb_male){ 
        Toast.makeText(MainActivity.</span><span class="s0">this</span><span class="s1">, </span><span class="s2">&quot;男&quot;</span><span class="s1">, Toast.LENGTH_SHORT).show(); 
    } 
    </span><span class="s0">if</span><span class="s1">(checkedId == R.id.rb_female){ 
        Toast.makeText(MainActivity.</span><span class="s0">this</span><span class="s1">, </span><span class="s2">&quot;女&quot;</span><span class="s1">, Toast.LENGTH_SHORT).show(); 
    } 
    } 
}); 
 
 
Button btnAlertDialog = findViewById(R.id.btn_alert_dialog); 
btnAlertDialog.setOnClickListener(</span><span class="s0">new </span><span class="s1">View.OnClickListener() { 
    @Override 
    </span><span class="s0">public void </span><span class="s1">onClick(View v) { 
 
        AlertDialog.Builder builder 
                = </span><span class="s0">new </span><span class="s1">AlertDialog.Builder(MainActivity.</span><span class="s0">this</span><span class="s1">); 
        builder.setTitle(</span><span class="s2">&quot;警告框&quot;</span><span class="s1">); 
      </span><span class="s3">//  builder.setMessage(&quot;你是否确认真的删除&quot;);</span><span class="s1"> 
        </span><span class="s3">/*builder.setSingleChoiceItems(furits, -1, new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
                Toast.makeText(MainActivity.this, furits[which], Toast.LENGTH_SHORT).show(); 
            } 
        });*/</span><span class="s1"> 
 
        builder.setMultiChoiceItems(furits, </span><span class="s0">null</span><span class="s1">, </span><span class="s0">new </span><span class="s1">DialogInterface.OnMultiChoiceClickListener() { 
            @Override 
            </span><span class="s0">public void </span><span class="s1">onClick(DialogInterface dialog, </span><span class="s0">int </span><span class="s1">which, </span><span class="s0">boolean </span><span class="s1">isChecked) { 
                Toast.makeText(MainActivity.</span><span class="s0">this</span><span class="s1">, furits[which]+isChecked, Toast.LENGTH_SHORT).show(); 
            } 
        }); 
        builder.setPositiveButton(</span><span class="s2">&quot;确认&quot;</span><span class="s1">, </span><span class="s0">new </span><span class="s1">DialogInterface.OnClickListener() { 
            @Override 
            </span><span class="s0">public void </span><span class="s1">onClick(DialogInterface dialog, </span><span class="s0">int </span><span class="s1">which) { 
                Toast.makeText(MainActivity.</span><span class="s0">this</span><span class="s1">, </span><span class="s2">&quot;确认被点击&quot;</span><span class="s1">, Toast.LENGTH_SHORT).show(); 
            } 
        }); 
               builder.setNegativeButton(</span><span class="s2">&quot;取消&quot;</span><span class="s1">, </span><span class="s0">new </span><span class="s1">DialogInterface.OnClickListener() { 
                   @Override 
                   </span><span class="s0">public void </span><span class="s1">onClick(DialogInterface dialog, </span><span class="s0">int </span><span class="s1">which) { 
                       Toast.makeText(MainActivity.</span><span class="s0">this</span><span class="s1">, </span><span class="s2">&quot;取消被点击&quot;</span><span class="s1">, Toast.LENGTH_SHORT).show(); 
                   } 
               }); 
               builder.setIcon(R.mipmap.ic_launcher); 
        AlertDialog dialog = builder.create(); 
        dialog.show(); 
    } 
}); 
 
} 
 
    </span><span class="s0">public void </span><span class="s1">click(View view) { 
        Toast.makeText(</span><span class="s0">this</span><span class="s1">, </span><span class="s2">&quot;按钮被点击了&quot;</span><span class="s1">, Toast.LENGTH_SHORT).show(); 
    } 
} 
</span></pre>
</body>
</html>